import api from '@/api/order'
import { Message } from 'element-ui';
import moment from 'moment';

const good = {
    state: {
        orderDetail: [],
        myOrder: [],
        myOrderFilter: [],
        order_idList: [],
        payActive: 0,
    },

    mutations: {
        saveOrderDetail: (state, orderDetail) => {
            state.orderDetail.splice(0,state.orderDetail.length);
            orderDetail.forEach((shop,index)=>{
                const pushGood = [];
                state.orderDetail.push({
                    shopName: shop.shop_name || shop.shopName,
                    shopId: shop.sellerId || shop.shopId,
                    orderGood:[]
                });
                shop.orderGood.forEach(good => {
                    pushGood.push({
                        goodId: good.id || good.goods_id,
                        imgUrl: good.imgUrl || good.goods2content_content,
                        goodName: good.name || good.goods_name,
                        goodType: good.type_name|| '模板',
                        goodPrice: good.price || good.goods_price 
                    })
                });
                state.orderDetail[index].orderGood = [...pushGood];
            });
        },
        saveMyOrder: (state, { allOrderInfo, expirationTime }) => {
            state.myOrder.splice(0,state.myOrder.length);
            let order_arr = [];
            allOrderInfo.forEach(order => {
                if(!order_arr.includes(order.order_id)){
                    order_arr.push(order.order_id);
                    state.myOrder.push({
                        order_id: order.order_id,
                        order_code: order.order_code,
                        order_createTime: moment(order.order_createTime).format('YYYY-MM-DD HH:mm:ss'),
                        shop_name: order.shop_name,
                        shop_id: order.shop_id,
                        orderGoods: [],
                        order_price: 0,
                        order_status: order.order_status,
                        hovered: false,
                        evaluated: false,
                        order_lostTime: order.order_status === 0 ? moment(order.order_createTime).add(expirationTime,'m').format('MM-DD HH:mm:ss')  : null
                    });
                }
            });
            allOrderInfo.forEach(order => {
                state.myOrder.forEach((item,index) => {
                    if(order.order_code === item.order_code){
                        state.myOrder[index].orderGoods.push(order);
                        state.myOrder[index].order_price += order.goods_price;
                    }
                })
            });
        },
        orderToDetail: (state,orderId) => {
            state.orderDetail.splice(0,state.orderDetail.length);
            const selectOrder = {};
            state.myOrder.forEach(order => {
                if(order.order_id === orderId)  {
                    selectOrder.order_id = orderId;
                    selectOrder.order_code = order.order_code
                    selectOrder.shopName = order.shop_name;
                    selectOrder.shopId = order.shop_id;
                    selectOrder.orderGood = [];
                    order.orderGoods.forEach(good => {
                        selectOrder.orderGood.push({
                            goodId: good.goods_id,
                            imgUrl: good.goods2title_title,
                            goodName: good.goods_name,
                            goodType: good.type_name|| '模板',
                            goodPrice: good.goods_price 
                        });
                    })
                }
            });
            state.orderDetail.push(selectOrder);
        },
        filterMyOrder: (state,type) => {
            state.myOrderFilter.splice(0,state.myOrderFilter.length);
            if(type !== 2){
                state.myOrderFilter = state.myOrder.filter(item => item.order_status === type);
            }else{
                state.myOrderFilter = state.myOrder.filter(item => item.order_status === type || item.order_status === 1);
            }
        },
        saveOrderID: (state, orderId) => {
            state.order_idList.splice(0,state.order_idList.length);
            state.order_idList.push(orderId);
        },
        changeActive: (state,payload) => {
            state.payActive = payload;
        },
        changeStatu: (state, payload) => {
            const { order_id, statu } = payload;
            state.myOrder.forEach(item => {
                if(item.order_id === order_id){
                    item.order_status  = statu;
                }
            });
            state.myOrderFilter.forEach((item,index) => {
                if(item.order_id === order_id){
                    item.order_status  = statu;
                    state.myOrderFilter.splice(index,1);
                }
            });
        }
    },

    actions: {
        //商品页结算
        async fetchAddOneGoodsToOrder({ commit },payload) {
            const { code, extend: { result: { order_id } } } = await api.addOneGoodsToOrder(payload.payload);
            if(code === 200){
                Message({
                    type: 'success',
                    message: '已为您生成订单',
                    showClose: true
                });
                commit('saveOrderID', order_id);
            }
            commit('saveOrderDetail', payload.orderDetail );
        },
        //购物车页结算
        async fetchAddGoodsToOrderByShopId({ commit },payload) {
            const { code, extend: { result: { order_createTime, order_id } } } = await api.addGoodsToOrderByShopId(payload);
            if(code === 200){
                Message({
                    type: 'success',
                    showClose: true,
                    message: '已为您生成订单'
                });
                commit('saveOrderID', order_id);
            }
        },
        // 获得订单信息
        async fetchGetOrderInfo({ commit },payload) {
            const { code } = await api.getOrderInfo(payload);
        },
        //付款
        async fetchPayForOrder({ commit },payload) {
            const { code, extend: {result} } = await api.payForOrder(payload);
            if( code === 200 ){
                Message({
                    type: 'success',
                    showClose: true,
                    message: '付款成功!'
                });
                commit('changeActive',2);
                commit('user/changeUsertotalPay', payload.order_totalMoney);
                commit('user/changeUserrestMoney', -payload.order_totalMoney);
            }else{
                Message({
                    type: 'error',
                    showClose: true,
                    message: result
                });
            }
        },
        //待支付状态付款
        async fetchPayForOrderById({ commit },payload) {
            const { code, extend: { result } } = await api.payForOrderById(payload);
            if( code === 200 ){
                Message({
                    type: 'success',
                    showClose: true,
                    message: '付款成功!'
                });
                commit('changeActive',2);
                commit('user/changeUsertotalPay', payload.order_totalMoney);
                commit('user/changeUserrestMoney', -payload.order_totalMoney);
            }else{
                Message({
                    type: 'error',
                    showClose: true,
                    message: result
                });
            }
        },
        //获得商品
        async fetchGetGoodsFile({ commit },payload) {
            const { code } = await api.getGoodsFile(payload);
        },
        //我的订单管理
        async fetchGetAllOrderInfo({ commit },payload) {
            const { code, extend: { result: { allOrderInfo, expirationTime } } } = await api.getAllOrderInfo(payload);
            if( code === 200 ){
                commit('saveMyOrder', { allOrderInfo, expirationTime });
            }
        },
        // 评价
        async fetchAddEvaluation({ commit },payload) {
            const { code } = await api.addEvaluation(payload.evaluation);
            if( code === 200 ){
                Message({
                    type: 'success',
                    showClose: true,
                    message: '您已完成评价,感谢!'
                });
                commit('changeStatu', { order_id: payload.order_id, statu: 2 })
            }else{
                Message({
                    type: 'error',
                    showClose: true,
                    message: '评价失败,请重试。'
                });
            }
        },
        //查看评价
        fetchEvaluationDetail({ commit },payload) {
            return api.EvaluationDetailByOrder(payload);
        },
        //删除
        async fetchDeleteOrderInfo({ commit },payload) {
            const { code } = await api.deleteOrderInfo(payload);
            if( code === 200 ){
                Message({
                    type: 'success',
                    message: '删除成功。',
                    showClose: true
                })
            }else{
                Message({
                    type: 'error',
                    message: '删除失败,请重试',
                    showClose: true
                })
            }
        },
    }
}

export default good